package com.jh.tools.common.http;

import com.apkfuns.logutils.LogUtils;
import com.jh.tools.api.Api;
import com.jh.tools.api.ApiService;
import com.jh.tools.api.manager.RetrofitManager;
import com.jh.tools.bean.ImagesListEntity;
import com.jh.tools.bean.MusicsListEntity;
import com.jh.tools.bean.NeteastNewsDetail;
import com.jh.tools.bean.NeteastNewsSummary;
import com.jh.tools.bean.NeteastVideoSummary;
import com.jh.tools.bean.SinaPhotoDetail;
import com.jh.tools.bean.SinaPhotoList;
import com.jh.tools.bean.VideosListUserEntity;

import java.util.List;
import java.util.Map;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by niejiahuan on 2016/3/16.
 * Url管理操作类
 */
public class HttpUrlManage {
    private static HttpUrlManage mHttpUrlManage;
    private RetrofitManager mRetrofitManager;
    public static HttpUrlManage HttpUrlManage(){
        if (null==mHttpUrlManage){
            mHttpUrlManage=new HttpUrlManage();
        }
        return mHttpUrlManage;
    }
    private  ApiService mApiService;
    /**
     * 20 datas per page
     */
    public static final int PAGE_LIMIT = 20;
    public void setmApiService(RetrofitManager retrofitManager,ApiService mApiService) {
        this.mApiService = mApiService;
        this.mRetrofitManager=retrofitManager;
    }

    public class UrlApi {
        public static final String WANGYI_URLS ="http://c.m.163.com/";
        public static final String BAIDU_IMAGES_URLS = "http://image.baidu.com/data/imgs";
        public static final String YOUKU_VIDEOS_URLS = "https://openapi.youku.com/v2/searches/video/by_keyword.json";
        public static final String YOUKU_USER_URLS = "https://openapi.youku.com/v2/users/show.json";
        public static final String DOUBAN_PLAY_LIST_URLS = "http://www.douban.com/j/app/radio/people";
        /**
         * 新浪图片新闻
         */
        public static final String SINA_PHOTO_HOST = "http://api.sina.cn/sinago/";
    }
    /**
     * 网易新闻列表 例子：http://c.m.163.com/nc/article/headline/T1348647909107/0-20.html
     * <p/>
     * 对API调用了observeOn(MainThread)之后，线程会跑在主线程上，包括onComplete也是，
     * unsubscribe也在主线程，然后如果这时候调用call.cancel会导致NetworkOnMainThreadException
     * 加一句unsubscribeOn(io)
     *
     * @param type      新闻类别：headline为头条,list为其他
     * @param id        新闻类别id
     * @param startPage 开始的页码
     * @return 被观察对象
     */
    public Observable<Map<String, List<NeteastNewsSummary>>> getNewsListObservable(String type, String id, int startPage) {
        return mApiService.getNewsList(this.mRetrofitManager.getCacheControl(), type, id, startPage)
                .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io());
    }

    /**
     * 网易新闻详情：例子：http://c.m.163.com/nc/article/BG6CGA9M00264N2N/full.html
     *
     * @param postId 新闻详情的id
     * @return 被观察对象
     */
    public Observable<Map<String, NeteastNewsDetail>> getNewsDetailObservable(String postId) {
        return mApiService.getNewsDetail(this.mRetrofitManager.getCacheControl(), postId).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()).unsubscribeOn(Schedulers.io());
    }

    /**
     * 新浪图片新闻列表 例子：http://api.sina.cn/sinago/list.json?channel=hdpic_pretty&adid=4ad30dabe134695c3b7c3a65977d7e72&wm=b207&from=6042095012&chwm=12050_0001&oldchwm=12050_0001&imei=867064013906290&uid=802909da86d9f5fc&p=1
     *
     * @param page 页码
     * @return 被观察对象
     */
    public Observable<SinaPhotoList> getSinaPhotoListObservable(String photoTypeId, int page) {
        LogUtils.e("新浪图片新闻列表: " + photoTypeId + ";" + page);
        return mApiService.getSinaPhotoList(this.mRetrofitManager.getCacheControl(), photoTypeId,
                "4ad30dabe134695c3b7c3a65977d7e72", "b207", "6042095012", "12050_0001",
                "12050_0001", "867064013906290", "802909da86d9f5fc", page)
                .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io());
    }

    /**
     * 新浪图片详情 例子：http://api.sina.cn/sinago/article.json?postt=hdpic_hdpic_toutiao_4&wm=b207&from=6042095012&chwm=12050_0001&oldchwm=12050_0001&imei=867064013906290&uid=802909da86d9f5fc&id=20550-66955-hdpic
     *
     * @param id 图片的id
     * @return 被观察者
     */
    public Observable<SinaPhotoDetail> getSinaPhotoDetailObservable(String id) {
        return mApiService.getSinaPhotoDetail(this.mRetrofitManager.getCacheControl(), Api.SINA_PHOTO_DETAIL_ID, "b207",
                "6042095012", "12050_0001", "12050_0001", "867064013906290", "802909da86d9f5fc", id)
                .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io());
    }

    /**
     * 网易视频列表 例子：http://c.m.163.com/nc/video/list/V9LG4B3A0/n/0-10.html
     *
     * @param id        视频类别id
     * @param startPage 开始的页码
     * @return 被观察者
     */
    public Observable<Map<String, List<NeteastVideoSummary>>> getVideoListObservable(String id, long startPage) {
        LogUtils.e("网易视频列表: " + id + ";" + startPage);
        return mApiService.getVideoList(this.mRetrofitManager.getCacheControl(), id, startPage)
                .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io());
    }

    /**
     * 获取图片信息
     * @param category 关键字
     * @param pageNum 页数
     * @return
     */
    public Observable<ImagesListEntity> getImagesListUrl(String category,String pageNum) {
        return mApiService.getImagesListUrl(this.mRetrofitManager.getCacheControl(), category,"全部",pageNum,""+PAGE_LIMIT,"1").subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()).unsubscribeOn(Schedulers.io());
    }

    /**
     * 获取视频信息
     * @param category 关键字
     * @param pageNum 页数
     * @return
     */
    public Observable<VideosListUserEntity> getVideosListUrl(String category, String pageNum) {
        return mApiService.getVideosListUrl(this.mRetrofitManager.getCacheControl(),category,pageNum,""+PAGE_LIMIT,"all","0","today","published","6ecd6970268b4c53").subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()).unsubscribeOn(Schedulers.io());
    }

    /**
     * 获取的类型编号
     * @param channel
     * @return
     */
    public Observable<MusicsListEntity> getDoubanMusicsPlayListUrl(String channel) {
        return mApiService.getDoubanMusicsPlayListUrl(this.mRetrofitManager.getCacheControl(),channel,"100","","0","radio_android").subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()).unsubscribeOn(Schedulers.io());
    }
}
